package com.ky.serialport.socket;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.ky.serialportpackage.R;
import com.ky.serialport.service.SocketService;
import com.ky.serialport.socket.listener.OnControlListener;
import com.ky.serialport.utils.MyFunc;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SocketActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    private String TAG = "Socket_inf";
    private Intent intent;
    private SocketService.MyBinder myBinder;
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myBinder = (SocketService.MyBinder) iBinder;
            myBinder.setOnControlListener(new OnControlListener() {
                @Override
                public void onConnectSuccess() {
                    Log.d(TAG, "onConnectSuccess: --------> 连接成功");
                }

                @Override
                public void onConnectFail(Exception e) {
                    Log.d(TAG, "onConnectFail: --------> 连接失败");
                }

                @Override
                public void onReconnectSuccess(int successConnectCount) {
                    Log.d(TAG, "onReconnectSuccess: --------> 重连成功");
                }

                @Override
                public void onReconnectFail(int failConnectCount) {
                    Log.d(TAG, "onReconnectFail: --------> 重连失败");
                }

                @Override
                public void onConnectBreak(int breakConnectCount) {
                    Log.d(TAG, "onConnectBreak: --------> 连接中断");
                }

                @Override
                public void onRead(byte[] buffer) {
                    Log.d(TAG, "onRead: --------> 长度：" +  buffer.length + "    " + MyFunc.ByteArrToHex(buffer));
                }

                @Override
                public void onSend(boolean statue, byte[] sendBuffer) {
                    Log.d(TAG, "onSend: --------> " +statue + "  ---  " + MyFunc.ByteArrToHex(sendBuffer));
                }
            });
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_socket);
        ButterKnife.bind(this);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        intent = new Intent();
        intent.setClass(this, SocketService.class);
        intent.putExtra("IP", "10.10.176.1");
        intent.putExtra("PORT", 4000);
        bindService(intent, connection, BIND_AUTO_CREATE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (intent != null) {
            stopService(intent);
        }
    }

    @OnClick(R.id.fab)
    public void onViewClicked() {
        if (myBinder!=null){
            myBinder.send(MyFunc.HexToByteArr("010300640C9C017C"));
        }
    }
}
